local UIBase = require("view.UIBase")
local ChatMiniView = class("ChatMiniView", UIBase)
local ChatMiniCell = require("view.chat.ChatMiniCell")

function ChatMiniView:ctor()
    local resource = {
        path = "Prefabs/View/Chat",
        name = "ChatMiniView",
        canvas = 1
    }
    ChatMiniView.super.ctor(self, resource)
end

function ChatMiniView:start()
    self._chatList = G_UserData:getChat():getAllChatMsgs()
    self._btnTouch.onClick:AddListener(handler(self, self._onBtnTouchClick))
    self._chatScroll:SetItemFunc(handler(self, self._getContentMsgcell))
    self._chatScroll:reloadData()
    self._signalChatMsg = G_SignalManager:add(SignalConst.EVENT_CHAT_MSG_UPDATE, handler(self, self._onEventChatMsg))
end

function ChatMiniView:_getContentMsgcell(index, parent)
    local startIndex = index + 1
    local msg = self._chatList[startIndex]
    if msg then
        local cell = ChatMiniCell.new(parent.gameObject, msg)
        return cell:getPanel()
    end
    return nil
end

function ChatMiniView:onDestroy()
    self._signalChatMsg:remove()
    self._signalChatMsg = nil
end

function ChatMiniView:_onEventChatMsg(id, msg)
    self._chatList = G_UserData:getChat():getAllChatMsgs()
    self._chatScroll:reloadData()
end

function ChatMiniView:_onBtnTouchClick()
    local msg = self._chatList[1]
    if msg then
        local user = {userId = msg:getPlayerId(), userName = msg:getThe_name()}
        local chat = require("view.chat.ChatView").new(user)
    end
end

return ChatMiniView
